﻿// mysql_test_1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <assert.h>
#include <vector>
#include "PS_mysql_pool.h"
#include "Thread.h"
#include "strCoding.h"

using namespace std;
using namespace mediakit;

string hello() {
	string str1 = "Hello BB";
	return str1;
}

//读取数据
bool Select(MYSQL_RES* Result, std::vector<std::vector<std::string> >& data)
{
	// 行列数
	int row = mysql_num_rows(Result);
	int field = mysql_num_fields(Result);

	MYSQL_ROW line = NULL;
	line = mysql_fetch_row(Result);

	int j = 0;
	std::string temp;
	std::vector<std::vector<std::string> >().swap(data);
	while (NULL != line)
	{
		std::vector<std::string> linedata;
		for (int i = 0; i < field; i++)
		{
			if (line[i])
			{
				temp = line[i];
				linedata.push_back(temp);
			}
			else
			{
				temp = "";
				linedata.push_back(temp);
			}
		}
		line = mysql_fetch_row(Result);
		data.push_back(linedata);
	}
	return true;
}

CMysqlPool* pMysql = nullptr;
int TestRunRead()
{
	while (true)
	{
#if 1
		int nNum = 0;
		string strSql = "";
		strSql += "SELECT * FROM tb_sys_log;";
		MYSQL_RES* pMysqlRes;
		pMysqlRes = pMysql->SelectRecord(strSql.c_str());
		//assert(pMysqlRes);
		nNum = pMysql->GetFieldNum(pMysqlRes);
		long long nllNum;
		nllNum = pMysql->GetRowNum(pMysqlRes);

		cout << nNum << endl;
		cout << nllNum << endl;
		cout << pMysql->GetClientInfo() << endl;

		for (size_t i = 0; i < nNum; i++)
		{
			cout << pMysql->GetFieldName(pMysqlRes, i) << endl;
		}

		std::vector<std::vector<std::string> > data;
		bool bRet = false;
		bRet = Select(pMysqlRes, data);
		if (!bRet)
		{
			std::cout << "Select error\n";
			return 0;
		}

		for (std::vector<std::vector<std::string> >::const_iterator iter = data.cbegin(); iter != data.cend(); iter++)
		{
			std::cout << "Hello World!\n";
			for (int i = 0; i < (*iter).size(); i++)
			{
				cout << strCoding::UTF8ToGB2312((*iter)[i]).c_str() << endl;
			}
		}
#endif

		printf("TestRunRead\n");
		Sleep(1000);
	}

	return 0;
}

int TestRunWrite()
{
	while (true)
	{
		string strSql = "";
		strSql += "INSERT INTO tb_sys_log VALUES (NULL,'192.168.0.106', '2022-05-06T14:58:16', '实时播放', '操作成功', 1, 1)";
		bool flag;

		flag = pMysql->InsertRecord(strSql.c_str());

		printf("TestRunWrite\n");
		Sleep(1000);
	}
	return 0;
}

int main()
{
	int nRet = 0;
	//CMysqlPool pMysql(1);
	//读写最少两个连接
	pMysql = new CMysqlPool(2);
	nRet = pMysql->PS_mysql_init("192.168.0.9", "root", "root", "stream_media", 0, NULL, 0);
	if (nRet != 0)
	{
		printf("PS_mysql_init error : %d\n", nRet);
		return 0;
	}

	CThread thre_read;
	thre_read.Start(TestRunRead);
	CThread thre_write;
	thre_write.Start(TestRunWrite);

	//string strSql = "";
	//strSql += "INSERT INTO tb_sys_log VALUES (NULL,'192.168.0.106', '2022-05-06T14:58:16', '实时播放', '操作成功', 1, 1)";
	//bool flag;

	//flag = pMysql.InsertRecord(strSql.c_str());
	//flag = pMysql.InsertRecord(strSql.c_str());
	//flag = pMysql.InsertRecord(strSql.c_str());
	//flag = pMysql.InsertRecord(strSql.c_str());
	//flag = pMysql.InsertRecord(strSql.c_str());

	while (true)
	{
		Sleep(1000);
	}

	return 0;
#if 0
	int nNum = 0;
	string strSql = "";
	strSql += "SELECT * FROM tb_sys_log;";
	MYSQL_RES* pMysqlRes;
	pMysqlRes = pMysql.SelectRecord(strSql.c_str());
	assert(pMysqlRes);
	nNum = pMysql.GetFieldNum(pMysqlRes);
	long long nllNum;
	nllNum = pMysql.GetRowNum(pMysqlRes);

	cout << nNum << endl;
	cout << nllNum << endl;
	cout << pMysql.GetClientInfo() << endl;

	for (size_t i = 0; i < nNum; i++)
	{
		cout << pMysql.GetFieldName(pMysqlRes, i) << endl;
	}

	std::vector<std::vector<std::string> > data;
	bool bRet = false;
	bRet = Select(pMysqlRes, data);
	if (!bRet)
	{
		std::cout << "Select error\n";
		return 0;
	}

	for (std::vector<std::vector<std::string> >::const_iterator iter = data.cbegin(); iter != data.cend(); iter++)
	{
		std::cout << "Hello World!\n";
		for (int i = 0; i < (*iter).size(); i++)
		{
			cout << (*iter)[i] << endl;
		}
	}


	return 0;
#endif
}